package Algorithm.BinarySearch;

public class BinarySearch {


    public static void main(String[] args) {

        int[] arr = {1, 2, 3, 6, 8, 9, 29};

        System.out.println(binarySearch(arr, 10));


    }


    public static int binarySearch(int[] arrays, int target) {

                        //注意边界条件
        int l = 0, r = arrays.length - 1;

        //注意边界条件
        while (l <= r) {

            int mid = (l + r) / 2;

/*            if (target < mid) {
                r = mid - 1;
            } else if (target > mid) {
                l = mid + 1;
            }else {
                return mid;
            }*/

            if (target == arrays[mid]) {
                return mid + 1;
            }

            if (target > mid) {
                l = mid + 1;
            } else {
                r = mid - 1;
            }


        }
        return -1;
    }


}
